<html>
<head><meta charset="utf-8"><title>Offsetting pointer to one struct field to another UB? · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html">Offsetting pointer to one struct field to another UB?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="177387615"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177387615" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177387615">(Oct 04 2019 at 23:29)</a>:</h4>
<p>Hi unsafe-code-guidelines. I raised <a href="https://github.com/rust-lang/rust/issues/65006" target="_blank" title="https://github.com/rust-lang/rust/issues/65006">#65006</a>, which details a way that a dataflow analysis I wrote for const qualification could be unsound in the presence of unsafe code. The soundness hole hinges upon taking a reference to one field of a struct on the stack, casting it to a raw pointer, and offsetting it to point at another, disjoint field of the same struct. You can see an <a href="https://github.com/rust-lang/rust/blob/master/src/test/ui/mir-dataflow/indirect-mutation-offset.rs" target="_blank" title="https://github.com/rust-lang/rust/blob/master/src/test/ui/mir-dataflow/indirect-mutation-offset.rs">example here</a></p>
<p>Is this example UB? According to the (non-normative) <a href="https://doc.rust-lang.org/std/primitive.pointer.html" target="_blank" title="https://doc.rust-lang.org/std/primitive.pointer.html"><code>pointer::offset</code> docs</a>:</p>
<blockquote>
<p>Both the starting and resulting pointer must be either in bounds or one byte past the end of the same allocated object. Note that in Rust, every (stack-allocated) variable is considered a separate allocated object.</p>
</blockquote>
<p>This suggests to me that this is not UB. However, it seems like this should in fact be UB, and users must take the address of the entire struct if they wish to do these kinds of shenanigans with <code>pointer::offset</code>.</p>



<a name="177424853"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177424853" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177424853">(Oct 05 2019 at 18:07)</a>:</h4>
<p>I believe that it is UB</p>



<a name="177424986"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177424986" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177424986">(Oct 05 2019 at 18:10)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> could you point me towards a reference? Nomicon, LLVM docs or a previous zulip conversation?</p>



<a name="177425003"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177425003" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177425003">(Oct 05 2019 at 18:11)</a>:</h4>
<p>If you have one at hand that is</p>



<a name="177425489"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177425489" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177425489">(Oct 05 2019 at 18:25)</a>:</h4>
<p>I believe I've seen the validity of <code>&amp;arr[0] as *const u8</code> vs <code>&amp;arr as *const u8</code> discussed here before, which is basically the same issue, but I can't seem to find it searching for "pointer offset"</p>



<a name="177425906"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177425906" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177425906">(Oct 05 2019 at 18:35)</a>:</h4>
<p><a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/134" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/134">rust-lang/unsafe-code-guidelines#134</a></p>



<a name="177426778"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177426778" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177426778">(Oct 05 2019 at 18:57)</a>:</h4>
<p>well I'm going by that discussion you just mentioned</p>



<a name="177426832"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177426832" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177426832">(Oct 05 2019 at 18:58)</a>:</h4>
<p>it's my understanding that the pointer to just that one field had its validity narrowed to just that field, it is no longer legal for the whole struct</p>



<a name="177426834"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177426834" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177426834">(Oct 05 2019 at 18:58)</a>:</h4>
<p>this is how "split borrows" work</p>



<a name="177426836"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177426836" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177426836">(Oct 05 2019 at 18:58)</a>:</h4>
<p>Yes, I was asking for a link to that discussion (I hadn't seen it before now)</p>



<a name="177426841"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177426841" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177426841">(Oct 05 2019 at 18:58)</a>:</h4>
<p>I closed the outstanding issue</p>



<a name="177426853"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177426853" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177426853">(Oct 05 2019 at 18:59)</a>:</h4>
<p>What's "split borrows"?</p>



<a name="177427351"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177427351" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177427351">(Oct 05 2019 at 19:12)</a>:</h4>
<p>when you borrow two fields of the same struct separately</p>



<a name="177427357"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177427357" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177427357">(Oct 05 2019 at 19:12)</a>:</h4>
<p>like how slice::split(...) works</p>



<a name="177427374"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177427374" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177427374">(Oct 05 2019 at 19:13)</a>:</h4>
<p>unfortunately i also cannot easily locate the pointer validity span discussion (on my phone right now)</p>



<a name="177427716"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177427716" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177427716">(Oct 05 2019 at 19:23)</a>:</h4>
<p>Ah okay, I understand</p>



<a name="177429577"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177429577" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177429577">(Oct 05 2019 at 20:18)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> a counter example could be arrays and borrowing of its first element, no?</p>



<a name="177429585"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177429585" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177429585">(Oct 05 2019 at 20:19)</a>:</h4>
<p>you still want to be able to iterate through the array by doing pointer arithmetic on the pointer.</p>



<a name="177429586"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177429586" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177429586">(Oct 05 2019 at 20:19)</a>:</h4>
<p>although at this point we hit <code>raw pointer</code> vs <code>reference</code>.</p>



<a name="177429675"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177429675" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177429675">(Oct 05 2019 at 20:21)</a>:</h4>
<p>I'm not sure that's a counter-example</p>



<a name="177429689"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177429689" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177429689">(Oct 05 2019 at 20:21)</a>:</h4>
<p>you need a reference to the whole array to make a legal reference to an element of the array</p>



<a name="177429748"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177429748" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177429748">(Oct 05 2019 at 20:22)</a>:</h4>
<p>once you have converted &amp;[T] into a particular &amp;T, you can't just jump that to another element, you have to go back to the full slice reference</p>



<a name="177429835"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177429835" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177429835">(Oct 05 2019 at 20:24)</a>:</h4>
<p>That’s definitely not something anybody does, though again, they usually use pointers, not references. Even the slice iterator is 2 <em>element</em> pointers and <code>fn next() { ...; start = ptr::offset(start, 1); ... }</code>.</p>



<a name="177429860"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177429860" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177429860">(Oct 05 2019 at 20:25)</a>:</h4>
<p>However for the particular optimisation <span class="user-mention" data-user-id="118594">@ecstatic-morse</span> cares about, they need to handle both pointers and references equally… well.</p>



<a name="177429907"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177429907" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177429907">(Oct 05 2019 at 20:26)</a>:</h4>
<p>(esp. since MIR does not yet have <code>raw &amp;</code> operation AFAIK)</p>



<a name="177429926"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177429926" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177429926">(Oct 05 2019 at 20:27)</a>:</h4>
<p>Nothing that was said conflicts with any of that, you just gotta use <code>slice.as_ptr()</code> (or copy its body) instead of <code>&amp;slice[0]</code> for creating the raw pointer.</p>



<a name="177429980"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177429980" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177429980">(Oct 05 2019 at 20:28)</a>:</h4>
<p>Fair.</p>



<a name="177713108"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177713108" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177713108">(Oct 09 2019 at 13:27)</a>:</h4>
<blockquote>
<p>Is this example UB? According to the (non-normative) pointer::offset docs:</p>
</blockquote>
<p>note that those docs just say when <code>pointer::offset</code> is UB; they do not specify when it is (not) UB to actually use the resulting ptr</p>



<a name="177713131"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177713131" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177713131">(Oct 09 2019 at 13:27)</a>:</h4>
<p>this is aliasing model territory which is very unspecified (Nomicon and Reference both explicitly keep that open)</p>



<a name="177713213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting%20pointer%20to%20one%20struct%20field%20to%20another%20UB%3F/near/177713213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Offsetting.20pointer.20to.20one.20struct.20field.20to.20another.20UB.3F.html#177713213">(Oct 09 2019 at 13:28)</a>:</h4>
<p><span class="user-mention" data-user-id="118594">@ecstatic-morse</span> ^</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>